﻿using LuDragon.Common.Utils;
using LuDragon.Data.Entities;
using LuDragon.IService.System;
using System;
using System.Web.Security;

namespace LuDragon.Service.System
{
    /// <summary>
    /// 用户服务类
    /// </summary>
    public class UserService : BaseService<Sys_User>, IUserService
    {

        private const string LoginSessionName = "2F650F9C97AC2F2F7902BC0C54ABF549";
        private const string LoginCookieName = "FB55F1E8471B792B84C43AA7FDE2E51E";

        /// <summary>
        /// 获取用户登录实体
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public Sys_User GetUser(string userName, string password)
        {
            return GetModel(a => a.UserName == userName && a.Password == password);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(int id)
        {
            return Delete(GetModel(a => a.UserId == id));
        }
        /// <summary>
        /// 启用用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool Enabled(int userId)
        {
            var user = GetModel(a => a.UserId == userId);
            user.Enabled = true;
            return Update(user);
        }
        /// <summary>
        /// 禁用用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool Disabled(int userId)
        {
            var user = GetModel(a => a.UserId == userId);
            user.Enabled = false;
            return Update(user);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool Save(Sys_User user)
        {
            if (user == null)
            {
                throw new Exception("实体参数为Null");
            }
            if (user.UserId > 0)
            {
                return Update(user);
            }
            else
            {
                if (Add(user).UserId > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }

        /// <summary>
        /// 获取当前登录用户
        /// </summary>
        /// <returns></returns>
        public Sys_User GetCurrentUser()
        {
            var user = SessionHelper.GetSession<Sys_User>(LoginSessionName);
            if (user == null)
            {
                var loginCookie = CookieHelper.GetCookie(LoginCookieName);
                if (string.IsNullOrEmpty(loginCookie))
                {
                    return null;
                }
                var userId = FormsAuthentication.Decrypt(loginCookie).Name;
                if (string.IsNullOrEmpty(userId))
                {
                    return null;
                }
                int uId = int.Parse(userId);
                user = GetModel(s => s.UserId == uId);
                SetLoginCookieSession(user);
                return user;
            }
            return user;
        }
        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public void LoginOut()
        {
            FormsAuthentication.SignOut();
            CookieHelper.DeleteCookie(LoginCookieName);
            SessionHelper.DeleteSession(LoginSessionName);
        }
        /// <summary>
        /// 设置登录Cookie和session
        /// </summary>
        /// <param name="user"></param>
        public void SetLoginCookieSession(Sys_User user)
        {
            FormsAuthentication.SetAuthCookie(user.UserId.ToString(), true, "/");
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, user.UserId.ToString(), DateTime.Now, DateTime.MaxValue, true, user.UserId.ToString());
            FormsIdentity identity = new FormsIdentity(ticket);
            CookieHelper.SetCookie(LoginCookieName, FormsAuthentication.Encrypt(ticket), 1);
            SessionHelper.SetSession(LoginSessionName, user);
        }
    }
}
